﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using MockupDesigner.Editors.ValidableEditors;
using Microsoft.Windows.Controls;

namespace MockupDesigner.Editors.Controls
{
    /// <summary>
    /// Interaction logic for DataGridEditorControl.xaml
    /// </summary>
    public partial class DataGridEditorControl : UserControl
    {
        public DataGridEditorControl(DataGridEditor editor)
        {
            InitializeComponent();
            this.editor = editor;
            table = new DataTable();

            for (int i = 0; i < 50; i++)
            {
                table.Columns.Add(new DataColumn());
            }
            for (int i = 0; i < 50; i++)
            {
                DataRow row = table.NewRow();

                table.Rows.Add(row);
            }
            dataGrid.ItemsSource = table.DefaultView;
            this.IsVisibleChanged += new DependencyPropertyChangedEventHandler(DataGridEditorControl_IsVisibleChanged);
        }

        void DataGridEditorControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (!((bool)e.NewValue) && !isCancel)
                editor.Validate();
        }

        private DataGridEditor editor;
        private DataTable table;

        public bool FirstLineAsHeaders
        {
            get
            {
                if (HeadersVisible.IsChecked.HasValue)
                    return HeadersVisible.IsChecked.Value;
                return false;
            }
            set
            {
                HeadersVisible.IsChecked = value;
            }
        }

        public string[][] Items
        {
            get
            {
                int[] lastIndexes = GetLastIndexes();
                int maxColumns = lastIndexes[0] + 1;
                int maxRows = lastIndexes[1] + 1;
                string[][] items = new string[maxColumns][];
                for (int i = 0; i < items.Count(); i++)
                {
                    items[i] = new string[maxRows];
                    for (int j = 0; j < maxRows; j++)
                    {
                        items[i][j] = table.Rows[j][i].ToString() == MockupDesigner.Items.Grid.DataGridHelper.EMPTY_CELL ? string.Empty : table.Rows[j][i].ToString();
                    }
                }
                return items;
            }
            set
            {
                string[][] items = value;
                int columns = items.Count();
                int rows = items[0].Count();

                for (int j = 0; j < rows; j++)
                {
                    for (int i = 0; i < columns; i++)
                    {
                        table.Rows[j][i] = items[i][j];
                    }
                }
            }
        }

        private int[] GetLastIndexes()
        {
            int rowIndex = 0;
            int colIndex = 0;

            int columns = table.Columns.Count;
            int rows = table.Rows.Count;
            for (int i = 0; i < columns; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    if (!string.IsNullOrEmpty(table.Rows[j][i].ToString()))
                    {
                        if (rowIndex < j)
                        {
                            rowIndex = j;
                        }
                        if (colIndex < i)
                        {
                            colIndex = i;
                        }
                    }
                }
            }
            return new int[] { colIndex, rowIndex };
        }

        private void OkBT_Click(object sender, RoutedEventArgs e)
        {
            editor.Validate();
        }

        private bool isCancel;
        private void CancelBT_Click(object sender, RoutedEventArgs e)
        {
            this.isCancel = true;
            editor.Cancel();
        }


    }
}
